const http = require("http");
const fs = require("fs");
const qs = require("querystring");
const url = require("url");

const serverObj = http.createServer((req,res)=>{
    const urlObj = url.parse(req.url);
    if(urlObj.pathname.includes("/api")){
        dataHandle(req, res);
    }else{
        staticHandle(req, res);
    }
})

serverObj.listen(3000,()=>{
    console.log("服务器开启成功: http://localhost:3000");
})


function dataHandle(req, res){
    console.log(req.method);
    if(req.method === "GET"){
        const obj = url.parse(req.url, true).query;
        fn(req, res, obj);
    }else if(req.method === "POST"){
        let str = "";
        req.on("data",(d)=>{
            str += d;
        })
        req.on("end",()=>{
            const obj = qs.parse(str);
            fn(req, res, obj);
        })
    }
}
function fn(req, res, reqData){
    switch(reqData.type){
        case "getGoods":
            getGoods(req, res, reqData);
            break;
        case "register":
            register(req, res, reqData);
            break;
        case "login":
            login(req, res, reqData);
            break;
    }
}
function login(req, res, reqData){
    fs.readFile("./databases/users.json","utf-8",(err,data)=>{
        const userMsg = err ? [] : (data ? JSON.parse(data) : []);
        const obj = {};
        let i = 0;
        const flag = userMsg.some((val,idx)=>{
            i = idx;
            return val.username === reqData.username;
        })
        if(flag){
            if(userMsg[i].password === reqData.password){
                obj.code = 1;
                obj.title = "登录成功";
                obj.data = userMsg[i].username;
            }else{
                obj.code = 2;
                obj.title = "登录失败，密码不符";
                obj.data = "NOTDATA";
            }
        }else{
            obj.code = 0;
            obj.title = "登录失败，用户名不存在";
            obj.data = "NOTDATA";
        }
        res.write(JSON.stringify(obj));
        res.end();
    })
}
function register(req, res, reqData){
    fs.readFile("./databases/users.json","utf-8",(err,data)=>{
        const userMsg = err ? [] : (data ? JSON.parse(data) : []);
        const obj = {};
        if(userMsg.some(val=>val.username === reqData.username)){
            obj.code = 0;
            obj.title = "注册失败，用户名重复";
            obj.data = "NOTDATA";
            res.write(JSON.stringify(obj));
            res.end();
        }else{
            userMsg.push({
                username:reqData.username,
                password:reqData.password
            })
            fs.writeFile("./databases/users.json", JSON.stringify(userMsg),err=>{
                obj.code = 1;
                obj.title = "注册成功";
                obj.data = "NOTDATA";
                res.write(JSON.stringify(obj));
                res.end();
            })
        }
    })
}
function getGoods(req, res, reqData){
    fs.readFile("./databases/goods.json",(err,data)=>{
        if(err){
            res.write("not data")
        }else{
            res.write(data)
        }
        res.end();
    })
}

function staticHandle(req, res){
    const urlObj = url.parse(req.url);
    // 要托管的静态资源的目录是dist
    fs.readFile("./dist" + urlObj.pathname, (err, data)=>{
        if(err){
            res.write("notFound");
        }else{
            res.write(data);
        }
        res.end();
    })
}
